import axios from 'axios'

const request = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {},
})

request.interceptors.request.use(function(config) {
  return config
})

// Add a response interceptor
let message = '未知错误，请联系管理员020-66666'
request.interceptors.response.use(
  function(response) {
    // console.log(response)
    // console.log(response.data)
    if (response.data.code === '200') {
      return response.data
    }
    if (response.data.code === '201') {
      return response.code
    }
    message = response.data.message
    return Promise.reject(message)
  },
  function(error) {
    return Promise.reject(message)
  }
)

export default request
